package org.yanex.apitest;

import java.util.List;

import org.yanex.project.api.API;
import org.yanex.project.api.APIFactory;
import org.yanex.project.api.APIFactory.Protocol;
import org.yanex.project.api.APIFactory.UnsupportedProtocolException;
import org.yanex.project.api.items.Category;
import org.yanex.project.api.items.City;
import org.yanex.project.api.items.Place;

public class Main {
	
	public static void main(String[] args) throws Exception {
		
		API _api;
		try {
			//localhost instance
			_api = APIFactory.getInstance().createAPI(
					"http://localhost/api/", Protocol.XML);
		} catch (UnsupportedProtocolException e) {
			System.err.println("Failed to create API object.");
			System.exit(0);
			return;
		}
		
		final API api = _api;
		
		new Thread() {
			public void run() {
				try {
					//test basic string methods
					System.out.println(api.getAbout());
					System.out.println(api.getContacts());
					
					//test cities & city categories
					List<City> cities = api.getCities();
					System.out.println("Cities ("+cities.size()+"):");
					for (City c : cities) {
						System.out.print(c.getName());
						List<Category> categories = api.getCategories(c.getId());
						System.out.print(c.getName()+" ("+categories.size()+"): ");
						for (Category cat : categories) {
							System.out.print(cat);
						}
						System.out.println();
					}
					
					//test a place method
					System.out.println("Place #0: "+api.getPlace(0));
					
					//text a places method
					List<Place> places = api.getPlaces(0);
					for (Place p : places) {
						System.out.println(p);
					}
					
				} catch (Exception e) {
					System.err.println("Exception caught.");
					System.exit(1);
				} finally {
					System.exit(0);
				}
			}
		}.start();
	}
	
}
